# -*-coding:utf8-*-#
import os
import sys
import time

#
# 列出设备
# adb devices
#
def me_list():
    return "devices"

#
# 获取屏幕分辨率
# adb shell wm size
#
def me_size():
    return "wm size"

#
# 启动Activity
# adb shell am start 包名/完整Activity路径
#
def me_start_app(app):
    return "am start {}".format(app)

#
# 查看前台Activity
# adb shell dumpsys activity activities | grep mFocusedActivity
#
def me_list_app():
    return "dumpsys activity activities"

#
# 屏幕截图
# adb shell screencap /sdcard/screen.png
#
def me_screencap(file):
    return "screencap {}".format(file)

#
# 录制视频
# adb shell screenrecord /sdcard/demo.mp4
#
def me_screenrecord(file):
    return "screenrecord {}".format(file)

###########################
##
##     文件操作
##
###########################

#
# 复制设备里的文件到电脑
# adb pull <设备里的文件路径> [电脑上的目录]
#
def me_download(src, dest):
    return "pull {} {}".format(src, dest)

#
# 复制电脑里的文件到设备
# adb push <电脑上的文件路径> <设备里的目录>
#
def me_upload(src, dest):
    return "push {} {}".format(src, dest)


###########################
##
##     模拟操作
##
###########################

#
# 输入
# adb shell input text <string> (Default: touchscreen)
#
def me_text(text):
    return "input text {}".format(text)

#
# 按键
# adb shell input keyevent [--longpress] <key code number or name> ... (Default: keyboard)
#
# | 0 - UNKNOWN       |
# | 1 - MENU          | 2 - SOFT_RIGHT     | 3 - HOME          | 4 - BACK         | 5 - CALL              |
# | 6 - ENDCALL       | 7 - 0              | 8 - 1             | 9 - 2            | 10 - 3                |
# | 11 - 4            | 12 - 5             | 13 - 6            | 14 - 7           | 15 - 8                |
# | 16 - 9            | 17 - STAR          | 18 - POUND        | 19 - DPAD_UP     | 20 - DPAD_DOWN        |
# | 21 - DPAD_LEFT    | 22 - DPAD_RIGHT    | 23 - DPAD_CENTER  | 24 - VOLUME_UP   | 25 - VOLUME_DOWN      |
# | 26 - POWER        | 27 - CAMERA        | 28 - CLEAR        | 29 - A           | 30 - B                |
# | 31 - C            | 32 - D             | 33 - E            | 34 - F           | 35 - G                |
# | 36 - H            | 37 - I             | 38 - J            | 39 - K           | 40 - L                |
# | 41 - M            | 42 - N             | 43 - O            | 44 - P           | 45 - Q                |
# | 46 - R            | 47 - S             | 48 - T            | 49 - U           | 50 - V                |
# | 51 - W            | 52 - X             | 53 - Y            | 54 - Z           | 55 - COMMA            |
# | 56 - PERIOD       | 57 - ALT_LEFT      | 58 - ALT_RIGHT    | 59 - SHIFT_LEFT  | 60 - SHIFT_RIGHT      |
# | 61 - TAB          | 62 - SPACE         | 63 - SYM          | 64 - EXPLORER    | 65 - ENVELOPE         |
# | 66 - ENTER        | 67 - DEL           | 68 - GRAVE        | 69 - MINUS       | 70 - EQUALS           |
# | 71 - LEFT_BRACKET | 72 - RIGHT_BRACKET | 73 - BACKSLASH    | 74 - SEMICOLON   | 75 - APOSTROPHE       |
# | 76 - SLASH        | 77 - AT            | 78 - NUM          | 79 - HEADSETHOOK | 80 - FOCUS            |
# | 81 - PLUS         | 82 - MENU          | 83 - NOTIFICATION | 84 - SEARCH      | 85 - TAG_LAST_KEYCODE |
def me_keyevent(keycode):
    return "input keyevent {}".format(keycode)

#
# 点击
# adb shell input tap <x> <y> (Default: touchscreen)
#
# （x, y)
def me_tap(x, y):
    return "input tap {} {}".format(x, y)

#
# 滑动
# adb shell input swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
#
def me_swipe(x1, y1, x2, y2, duration = 300):
    # input touchscreen swipe 930 880 930 380 //向上滑
    # input touchscreen swipe 930 880 330 880 //向左滑
    # input touchscreen swipe 330 880 930 880 //向右滑
    # input touchscreen swipe 930 380 930 880 //向下滑
    return "input swipe {} {} {} {} {}".format(x1, y1, x2, y2, duration)

###########################
##
##     运行
##
###########################
#  - adb -s deviceID command # execute command for device
# shell
def adb_run_shell(cmd, deviceID = None):
    device = ""
    if deviceID is not None:
        device = " -s {} ".format(deviceID)
    os.system(os.path.join(os.path.abspath("."), "bin", "adb.exe") + device + " shell " + cmd)

def adb_run(cmd, deviceID = None):
    device = ""
    if deviceID is not None:
        device = " -s {} ".format(deviceID)
    os.system(os.path.join(os.path.abspath("."), "bin", "adb.exe") + device + " " + cmd)

###########################
##
##     主程序
##
###########################
def main():
    # list devices
    # os.system(os.path.join(os.path.abspath("."), "bin", "adb.exe") + " devices")
    adb_run(me_list())
    adb_run_shell(me_size())
    # swipe screen
    while True:
        time.sleep(10)
        adb_run_shell(me_swipe(930, 880, 930, 380))
        # adb_run_shell(me_swipe(540, 880, 540, 380))

if __name__ == '__main__':
    main()